package com.web;

import com.bean.Phone;
import com.dao.PhoneDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/phone/list")
public class PhoneListServlet extends HttpServlet {
    /**
     * condition 条件的意思
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            // 创建 phone 集合，然后值默认为 null
            List<Phone> phones = null;
            // 查询所有信息赋值给 phones
            phones = new PhoneDao().list();

            // 把数据存到 iphone 中
            req.setAttribute("iphone", phones);
            // 请求转发
            req.getRequestDispatcher("/phone/phoneList.jsp").forward(req, resp);
        } catch (Exception e) {
            // 把异常信息打印到控制台
            e.printStackTrace();
            // 把失败信息存到 message 中
            req.setAttribute("message", "查询失败！");
            // 请求转发
            req.getRequestDispatcher("/phone/message.jsp").forward(req, resp);
        }
    }
}
